<?php
namespace app\index\controller;
use app\index\controller\BaseController;
use app\index\model\Project;
use app\index\model\PushProject;
use app\libs\Functions;
use Curl\Curl;
use think\Config;
use think\Db;
use think\exception\PDOException;
use think\Request;

/**
 * CRM 我的项目
 * @package app\index\controller
 */
class CrmProject extends BaseController{

    /**
     ** list 项目
     * @return false|string
     */
    public function listProject(){
       $reqData['is_monitor'] = $this->request->get('is_monitor');
       $reqData['follow_status'] = $this->request->get('follow_status');
       $reqData['customer'] = $this->request->get('customer');
       $reqData['keywords'] = $this->request->get('keywords');
       $reqData['beginTime'] = $this->request->get('beginTime');
       $reqData['endTime'] = $this->request->get('endTime');
       $page = $this->request->get('page',0);
       $limit = $this->request->get('limit',10);
       $dispatch = $this->request->dispatch();

       $result = $this->getBusiness('Crm')->listProject($reqData,$dispatch,$page,$limit);
       return json_encode($result);
    }

    /**
     * add 项目
     * @return false|string
     */
    public function addProject(){
        $reqData['s_type'] = $this->request->post('s_type');
        $reqData['identification'] = $this->request->post('identification');
        $reqData['project_name'] = $this->request->post('project_name');
        $reqData['contact'] = $this->request->post('contact');
        $reqData['phone'] = $this->request->post('phone');
        $reqData['email'] = $this->request->post('email');
        $reqData['follow_status'] = $this->request->post('follow_status');
        $reqData['customer'] = $this->request->post('customer');
        $reqData['remark'] = $this->request->post('remark');
        $dispatch = $this->request->dispatch();
        // 表单验证
        $validata = $this->validate($reqData,'app\index\validate\CrmAddProject');
        if (true !== $validata) {
            // 验证失败 输出错误信息
            $this->result['status'] = -1;
            $this->result['msg'] = $validata;
            return json_encode($this->result);
        }

        $result = $this->getBusiness('Crm')->addProject($reqData,$dispatch);
        return json_encode($result);
    }

    /**
     * 编辑项目
     * @return false|string
     */
    public function editProject(){
        $reqData['project_id'] = $this->request->post('project_id');
        $reqData['project_name'] = $this->request->post('project_name');
        $reqData['contact'] = $this->request->post('contact');
        $reqData['phone'] = $this->request->post('phone');
        $reqData['email'] = $this->request->post('email');
        $reqData['follow_status'] = $this->request->post('follow_status');
        $reqData['customer'] = $this->request->post('customer');
        $reqData['is_monitor'] = $this->request->post('is_monitor');
        $reqData['remark'] = $this->request->post('remark');
        $dispatch = $this->request->dispatch();
        // 表单验证
        $validata = $this->validate($reqData,'app\index\validate\CrmEditProject');
        if (true !== $validata) {
            // 验证失败 输出错误信息
            $this->result['status'] = -1;
            $this->result['msg'] = $validata;
            return json_encode($this->result);
        }

        $result = $this->getBusiness('Crm')->editProject($reqData,$dispatch);
        return json_encode($result);
    }

    /**
     * 项目详情
     * @return false|string
     */
    public function detailProject()
    {
        $reqData['id'] = $this->request->get('id');
        $dispatch = $this->request->dispatch();

        if(!preg_match("/^[1-9][0-9]*$/",$reqData['id'])){
            $this->result['status'] = -1;
            $this->result['msg'] = '参数不合法';
            return json_encode($this->result);
        }

        $result = $this->getBusiness('Crm')->detailProject($reqData['id'],$dispatch);
        return json_encode($result);
    }

    /**
     * 项目日志
     * @return false|string
     */
    public function logProject()
    {
        $reqData['id'] = $this->request->get('id');
        $page = $this->request->get('page');
        $limit =  $this->request->get('limit');
        $dispatch = $this->request->dispatch();

        if(!preg_match("/^[1-9][0-9]*$/",$reqData['id'])){
            $this->result['status'] = -1;
            $this->result['msg'] = '参数不合法';
            return json_encode($this->result);
        }

        $result = $this->getBusiness('Crm')->logProject($reqData['id'],$dispatch,$page,$limit);
        return json_encode($result);
    }

    /**
     * 监控项目 取消监控
     * @return false|string
     */
    public function monitorProject()
    {
        /***监控和提交监控信息**/
        $reqData['project_id']=$this->request->get('project_id',0,'intval');
        $reqData['company_name']=$this->request->get('company_name','','trim');
        $reqData['phone']=$this->request->get('phone','','trim');
        $reqData['email']=$this->request->get('email','','trim');
        /**取消监控**/
        $cancel['monitor_status']=$this->request->get('monitor_status',0,'intval');
        $cancel['project_id']=$this->request->get('id',0,'intval');
        $dispatch = $this->request->dispatch();

        if (!isset($dispatch['user_no'])){
            return json_encode(array('status'=>-1,'msg'=>'error','data'=>[]));
        }
        if ($cancel['monitor_status']==2){
            $row=db('project')->where(['status'=>1,'id'=>$cancel['project_id']])->update(['is_monitor'=>2]);
            if ($row==1){
                return json_encode(array('status'=>1,'msg'=>'取消监控成功','data'=>[]));
            }
            exit();
        }

        /*****修改数据库******/
        $reqData['user_no']=$dispatch['user_no'];
        $reqData['status']=3;
        Db::startTrans();
        try {
            db('project')->where(['status'=>1,'id'=>$reqData['project_id']])->update(['is_monitor'=>3]);
            db('project_monit')->insert($reqData);
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            $msg = "File：" . $e->getFile() . "\n";
            $msg .= "Line：" . $e->getLine() . "\n";
            $msg .= "Message：" . $e->getMessage() . "\n";
            var_dump($msg);
            // 回滚事务
            Db::rollback();
            $this->result['status'] = -1;
            $this->result['msg'] = '失败';
            return $this->result;
        }
        return json_encode(array('status' => 1, 'msg' => 'success', 'data' => []));
    }

    /**
     * 删除项目
     * @return false|string
     *
     */
    public function delProject()
    {
        $reqData['id'] = $this->request->get('id');
        $dispatch = $this->request->dispatch();
        if (!preg_match("/^[1-9][0-9]*$/", $reqData['id'])) {
            $this->result['status'] = -1;
            $this->result['msg'] = '参数不合法';
            return json_encode($this->result);
        }

        $result = $this->getBusiness('Crm')->delProject($reqData['id'],$dispatch);
        return json_encode($result);
    }

    /**
     * 部门列表
     * @return false|string
     * @throws
     */
    public function crmDepartment(){
        $dispatch = $this->request->dispatch();
        if (!isset($dispatch['user_no'])){
            $this->result['status'] = -1;
            $this->result['msg'] = 'error';
            return json_encode($this->result);
        }
        /****用户角色****/
        $dpt=db('user a')
            ->where(['a.status'=>1,'a.user_no'=>$dispatch['user_no']])
            ->join('position b','a.rolo_id=b.id','LEFT')
            ->field('b.belong,b.id,a.rolo_id')
            ->find();

        /****所属部门****/
        $res = Db::table('cq_department')->select()->toArray();
        /****子集和父级目录****/
        //$p_data=get_top_parentid($res,$dpt['belong']);
        $result = tree_children($res, $dpt['id']);
        //$res=array_merge($p_data,$s_data);
        $res = Db::table('cq_department')->where('id',$dpt['belong'])->find();
//        dd($result);
        $res['children']=$result;
        $this->result['status'] = 1;
        $this->result['msg'] = 'success';
        $this->result['data'][] = $res;
        return json_encode($this->result);
    }

    /**
     * 全部项目list
     * @return false|string
     * @throws
     */
    public function allProject(){
        $reqData['department_id'] = $this->request->get('department_id');
        $page = $this->request->get('page',0);
        $limit = $this->request->get('limit',0);
        $dispatch = $this->request->dispatch();

        $result = $this->getBusiness('Crm')->allProject($reqData['department_id'],$dispatch,$page,$limit);
        return json_encode($result);
    }

    /**
     * 成员列表list
     * @return false|string
     */
    public function memberList()
    {
        $reqData['department_id'] = $this->request->get('department_id');
        $page = $this->request->get('page',0);
        $limit = $this->request->get('limit',0);
        $dispatch = $this->request->dispatch();
        if (!preg_match("/^[1-9][0-9]*$/", $reqData['department_id'])) {
            $this->result['status'] = -1;
            $this->result['msg'] = '参数不合法';
            return json_encode($this->result);
        }
        $result = $this->getBusiness('Crm')->memberList($reqData['department_id'],$dispatch,$page,$limit);
        return json_encode($result);
    }

    /**
     * 推送项目list
     * @return false|string
     */
    public function crmPushProject()
    {
        $reqData['page'] = $this->request->get('page', 1);
        $reqData['limit'] = $this->request->get('limit', 10);
        $reqData['keywords'] = $this->request->get('keywords');
        $dispatch = $this->request->dispatch();
        $result = $this->getBusiness('Crm')->crmPushProject($dispatch,$reqData['page'],$reqData['limit'],$reqData['keywords']);
        // 我的收藏字段
        $return_data = $this->getBusiness('Common')->clickCollet($dispatch,$result['data'],Config::get('collect.7'));
        $result['data'] = $return_data;
        return json_encode($result);
    }
    
    /**
     * 推送项目detail
     */
    public function crmPushProjectDetail()
    {
        $reqData['id'] = $this->request->get('identification');
        $dispatch = $this->request->dispatch();
        $result = $this->getBusiness('Crm')->crmPushProjectDetail($reqData['id'],$dispatch);
        if($result['status'] == 1){
            $return_data = $this->getBusiness('Common')->clickCollet($dispatch,array($result['data']),Config::get('collect.7'));
            $result['data'] = $return_data;
        }
        return json_encode($result);
    }

    /**
     * 提供api添加推送项目
     * todo 还未做表单验证
     */
    public function crmPushProjectAdd(){
        $reqData['project_name'] = $this->request->post('project_name');
        $reqData['content'] = $this->request->post('content');
        $reqData['image'] = $this->request->post('image');
        $reqData['industry'] = $this->request->post('industry');
        $reqData['cooperation'] = $this->request->post('cooperation');
        $reqData['amount'] = $this->request->post('amount');
        $reqData['city'] = $this->request->post('city');

        $result = $this->getBusiness('Crm')->crmPushProjectAdd($reqData);
        return json_encode($result);
    }

    /**
     * add 首页项目库
     * @return false|strings
     * todo 还未做表单验证
     */
    public function projectLibAdd(){
        $reqData['phone'] = $this->request->post('phone');
        $reqData['project_name'] = $this->request->post('project_name');
        $reqData['content'] = $this->request->post('content');
        $reqData['desc'] = $this->request->post('desc');
        $reqData['industry'] = $this->request->post('industry');
        $reqData['industry_id'] = $this->request->post('industry_id');
        $reqData['cooperation'] = $this->request->post('cooperation');
        $reqData['amount'] = $this->request->post('amount');
        $reqData['image'] = $this->request->post('image');
        $reqData['grow'] = $this->request->post('grow',0,'trim');
        $dispatch = $this->request->dispatch();

        $result = $this->getBusiness('Crm')->addProjectLib($reqData,$dispatch);
        return json_encode($result);
    }

    /**
     * list 首页项目库列表
     * @return false|string
     */
    public function projectLibList(){
        $reqData['keywords'] = $this->request->get('keywords');
        $page = $this->request->get('page');
        $limit = $this->request->get('limit');
        $dispatch = $this->request->dispatch();

        $result = $this->getBusiness('Crm')->projectLibList($reqData,$dispatch,$page,$limit);
        return json_encode($result);
    }

    /**
     * detail 首页项目库详情
     * @return false|string
     */
    public function projectLibDetail(){
        $reqData['id'] = $this->request->get('id');
        $dispatch = $this->request->dispatch();
        $result = $this->getBusiness('Crm')->projectLibDetail($reqData['id'],$dispatch);
        return json_encode($result);
    }

    /**
     * edit 首页项目库编辑
     * @return false|string
     */
    public function projectLibEdit(){
        $reqData['id'] = $this->request->post('id');
        $reqData['phone'] = $this->request->post('phone');
        $reqData['project_name'] = $this->request->post('project_name');
        $reqData['content'] = $this->request->post('content');
        $reqData['industry'] = $this->request->post('industry');
        $reqData['industry_id'] = $this->request->post('industry_id');
        $reqData['cooperation'] = $this->request->post('cooperation');
        $reqData['amount'] = $this->request->post('amount');
        $reqData['image'] = $this->request->post('image');
        $reqData['grow'] = $this->request->post('grow',0,'trim');
        $dispatch = $this->request->dispatch();

        $result = $this->getBusiness('Crm')->projectLibEdit($reqData,$dispatch);
        return json_encode($result);
    }

    /**
     * del 首页项目库删除
     * @return false|string
     */
    public function projectLibDel(){
        $reqData['id'] = $this->request->get('id');
        $dispatch = $this->request->dispatch();

        if (!preg_match("/^[1-9][0-9]*$/", $reqData['id'])) {
            $this->result['status'] = -1;
            $this->result['msg'] = '参数不合法';
            return json_encode($this->result);
        }
        $result = $this->getBusiness('Crm')->projectLibDel($reqData['id'],$dispatch);
        return json_encode($result);
    }

    /**
     * list 首页项目库在首页展示 list
     * @return false|string
     */
    public function indexProjectList()
    {
        $reqData['city_id'] = $this->request->get('city_id');
        $reqData['industry_id'] = $this->request->get('industry_id');
        $reqData['cooperation'] = $this->request->get('cooperation');
        $reqData['amount_scop_id'] = $this->request->get('amount_scop_id');
        $reqData['keywords'] = $this->request->get('keywords');
        $page = $this->request->get('page');
        $limit = $this->request->get('limit');

        $result = $this->getBusiness('Crm')->indexProjectList($reqData,$page,$limit);
        return json_encode($result);
    }

    /**
     * detail 首页项目库在首页展示 detail
     * @return false|string
     */
    public function indexProjectDetail()
    {
        $reqData['id'] = $this->request->get('id');
        $result = $this->getBusiness('Crm')->indexProjectDetail($reqData['id']);
        return json_encode($result);
    }
    
    /**
     * 新闻资讯 add
     */
    public function newsAdd(){
        $reqData['news_name'] = $this->request->post('news_name');
        $reqData['news_origin'] = $this->request->post('news_origin');
        $reqData['content'] = $this->request->post('content');
        $reqData['image'] = $this->request->post('image');
        $reqData['industry'] = $this->request->post('industry');
        $reqData['industry_id'] = $this->request->post('industry_id');
        $reqData['chain'] = $this->request->post('chain');
        $reqData['chain_id'] = $this->request->post('chain_id');
        $reqData['grow'] = $this->request->post('grow',0,'trim');
        $dispatch = $this->request->dispatch();

        $result = $this->getBusiness('Crm')->newsAdd($reqData,$dispatch);
        return json_encode($result);
    }

    /**
     * 新闻资讯 list
     * @return false|string
     */
    public function newsList()
    {
        $reqData['keywords'] = $this->request->get('keywords');
        $page = $this->request->get('page');
        $limit = $this->request->get('limit');
        $dispatch = $this->request->dispatch();

        $result = $this->getBusiness('Crm')->newsList($reqData,$dispatch,$page,$limit);
        return json_encode($result);
    }

    /**
     * 新闻资讯 detail
     * @return false|string
     */
    public function newsDetail()
    {
        $reqData['identification'] = $this->request->get('identification');
        $dispatch = $this->request->dispatch();
        $result = $this->getBusiness('Crm')->newsDetail($reqData['identification'],$dispatch);
        return json_encode($result);
    }
    /**
     * 新闻资讯 edit
     * @return false|string
     */
    public function newsEdit()
    {
        $reqData['identification'] = $this->request->post('identification');
        $reqData['news_name'] = $this->request->post('news_name');
        $reqData['news_origin'] = $this->request->post('news_origin');
        $reqData['content'] = $this->request->post('content');
        $reqData['image'] = $this->request->post('image');
        $reqData['industry'] = $this->request->post('industry');
        $reqData['industry_id'] = $this->request->post('industry_id');
        $reqData['chain'] = $this->request->post('chain');
        $reqData['chain_id'] = $this->request->post('chain_id');
        $reqData['grow'] = $this->request->post('grow',0,'trim');
        $dispatch = $this->request->dispatch();

        $result = $this->getBusiness('Crm')->newsEdit($reqData,$dispatch);
        return json_encode($result);
    }

    /**
     * 新闻资讯 del
     * @return false|string
     */
    public function newsDel()
    {
        $reqData['identification'] = $this->request->get('identification');
        $dispatch = $this->request->dispatch();
        $result = $this->getBusiness('Crm')->newsDel($reqData['identification'],$dispatch);
        return json_encode($result);
    }

    /**
     * 新闻资讯首页展示 list
     * @return false|string
     */
    public function indexNewsList()
    {
        $page = $this->request->get('page',0);
        $limit = $this->request->get('limit',0);

        $result = $this->getBusiness('Crm')->indexNewsList($page,$limit);
        return json_encode($result);
    }

    /**
     * 新闻资讯首页展示 detail
     * @return false|string
     */
    public function indexNewsDetail(){
        $reqData['identification'] = $this->request->get('identification',0);
        $result = $this->getBusiness('Crm')->indexNewsDetail($reqData['identification']);
        return json_encode($result);
    }

    /**
     * 企业深度分析
     * @return false|string
     */
    public function companyAnalyze(){
        $reqData['company_name'] = $this->request->post('company_name');
        $reqData['phone'] = $this->request->post('phone');
        $reqData['email'] = $this->request->post('email');
        $reqData['project_id']=$this->request->post('project_id');
        $dispatch = $this->request->dispatch();

        $result = $this->getBusiness('Crm')->companyAnalyze($reqData,$dispatch);
        return json_encode($result);
    }

    /**
     * 企业深度分析报告列表展示
     */
    public function analyzeList()
    {
        $reqData['keyword']=$this->request->param('keyword','','trim');
        $page=$this->request->param('page',1,'intval');
        $limit=$this->request->param('limit',15,'intval');
        $dispatch = $this->request->dispatch();
        if (!isset($dispatch['user_no'])){
            return json_encode(array('status'=>-1,'msg'=>'error','data'=>[]));
        }
        $obj=db('company_analyze');
        if (!empty($reqData['keyword'])){
            $obj->where('user_no',$dispatch['user_no']);
        }
        $status=[
            0 => '待处理',
            2 => '完成'
        ];
        $result = $obj->order('updated_at','desc')->where(['user_no'=>$dispatch['user_no']])->paginate($limit)->toArray();
        foreach ($result['data'] as &$val){
            $val['status']=$status[$val['status']];
        }
        return json_encode(array('status'=>1,'msg'=>'error','data'=>$result));
    }

    /**
     * crm权限菜单list
     */
    public function crmMenuList(){
        $dispatch = $this->request->dispatch();
        if (!isset($dispatch['user_no'])){
            return json_encode(array('status'=>-1,'msg'=>'error','data'=>[]));
        }
        /********拥有菜单ids********/
        $ids=db('rolo_admin a')
            ->join('user b','a.id=b.admin_id')
            ->where(['a.status'=>1,'b.status'=>1,'user_no'=>$dispatch['user_no']])
            ->value('have_menu');
        /********拥有菜单list********/
        $menus=db('menu')
            ->field('id,p_id,menu_name')
            ->where('status',1)
            ->whereIn('id',$ids)
            ->select()
            ->toArray();
        $menus=$this->getBusiness('Common')->betree($menus);
        /********关注和项目的数量 推送项目 已读未读的数量********/
        $left=array();
        for ($i=1;$i<=7;$i++){
            //选择表名
            $tbl=Config::get('collect.'.$i);
            $left['collect'][$tbl]=db('user_collect_type'.$tbl)->where('status',1)->where('user_no',$dispatch['user_no'])->count();
        }
        $left['collect']=array_sum($left['collect']);
        $left['project']=db('project')->where(['status'=>1,'user_no'=>$dispatch['user_no']])->count();
        $left['push']=db('push_user_chain')->where(['status'=>1,'user_no'=>$dispatch['user_no'],'is_read'=>0])->count();
        return json_encode(array('status'=>1,'msg'=>'success','data'=>$menus,'count'=>$left));
    }

    /**
     * 所有部门和职位列表
     */
    public function crmDptPosition()
    {
        $position= db('position')->field('id,position_name,p_id')->select()->toArray();
        //$position=$this->getBusiness('Common')->betree($position);
        return json_encode(array('status'=>1,'msg'=>'success','data'=>$position));
    }

    /**
     * 获取当前角色下的所有用户
     */
    public function roloUserList()
    {
        $dispatch = $this->request->dispatch();
        if (!isset($dispatch['user_no'])){
            json_encode(array('status'=>-1,'msg'=>'error','data'=>[]));
        }
        /****子类权限****/
        $admin_id=db('user')->where(['status'=>1,'user_no'=>$dispatch['user_no']])->value('admin_id');
        $admin=db('rolo_admin')->where('status',1)->field('id,pid')->select()->toArray();
        $ids=get_all_child($admin,$admin_id);
        array_push($ids,-1);//新用户
        /****当前权限下的用户列表****/
        $obj=db('user a')
            ->field('a.user_no,a.vip,a.username,b.name as rolo_name,c.position_name,d.dpt_name')
            ->join('rolo_admin b','a.admin_id=b.id','LEFT')
            ->join('position c','a.rolo_id=c.id','LEFT')
            ->join('department d','c.belong=d.id','left')
            ->where(['a.status'=>1])
            ->whereIn('a.admin_id',$ids);
        if ($admin_id==1){
            $obj->whereOr('user_no',$dispatch['user_no']);
        }
        $user=$obj->order('a.admin_id', 'asc')->select()->toArray();

        /****所拥有角色的列表****/
        $rolo=db('rolo_admin')
            ->field('id,name,have_menu')
            ->where('status',1)
            ->whereIn('id',$ids)
            ->order('id','asc')
            ->select()
            ->toArray();
        foreach ($rolo as &$value){
            $value['menu']=db('menu')->where('status',1)->whereIn('id',$value['have_menu'])->column('menu_name');
            $value['menu']=implode(',',$value['menu']);
            unset($value['have_menu']);
        }
        $result=compact('user','rolo');
        return json_encode(array('status'=>1,'msg'=>'success','data'=>$result));

    }

    /**
     * 用户详情
     */
    public function crmUserDetail()
    {
        $user_no=$this->request->get('user_no',null,'intval');
        $dispatch = $this->request->dispatch();
        if (!isset($dispatch['user_no'])){
            json_encode(array('status'=>-1,'msg'=>'error','data'=>[]));
        }
        $user=db('user')->where(['status'=>1,'user_no'=>$user_no])->find();
        if (empty($user)){
            json_encode(array('status'=>-1,'msg'=>'error','data'=>[]));
        }
        return json_encode(array('status'=>1,'msg'=>'success','data'=>$user));
    }

    /**
     * crm给用户分配菜单权限
     */
    public function crmMenuSelect()
    {
        $reqData['admin_id']=$this->request->get('admin_id',null,'intval');
        $reqData['username']=$this->request->get('username','','trim');
        $reqData['phone']=$this->request->get('phone',0,'intval');
        $reqData['password']=$this->request->get('password',0,'trim');
        $reqData['position_id']=$this->request->get('rolo_id',null,'intval');
        $reqData['user_no']=$this->request->get('user_no',0,'intval');
        $reqData['vip']=$this->request->get('vip',0,'intval');
        $dispatch = $this->request->dispatch();
        if (!isset($dispatch['user_no'])){
            json_encode(array('status'=>-1,'msg'=>'error','data'=>[]));
        }
        /*****整理数据*****/
        $pwd=db('user')->where('user_no',$reqData['user_no'])->value('password');
        if ($pwd!=$reqData['password']){
            $upd['password_salt'] = Functions::uniqueString('64');
            $upd['password'] = Functions::encryptPassword($reqData['password'], $upd['password_salt'] );
        }
        $upd['username']=$reqData['username'];
        $upd['phone']=$reqData['phone'];
        $upd['admin_id']=$reqData['admin_id'];
        $upd['rolo_id']=$reqData['position_id'];
        $upd['vip']=$reqData['vip'];
        $upd['updated_at']=time();
        /*****修改数据库******/
        Db::startTrans();
        try {
            db('user')->where('user_no',$reqData['user_no'])->update($upd);
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            $msg  = "File：" . $e->getFile() . "\n";
            $msg .= "Line：" . $e->getLine() . "\n";
            $msg .= "Message：" . $e->getMessage() . "\n";
            var_dump($msg);
            // 回滚事务
            Db::rollback();
            $this->result['status'] = -1;
            $this->result['msg'] = 'update失败';
            return $this->result;
        }
        return json_encode(array('status'=>1,'msg'=>'success','data'=>[]));
    }

    /**
     * crm添加角色
     */
    public function crmAddRolo()
    {
        $reqData['id']=$this->request->post('id',null,'intval');
        $reqData['name']=$this->request->post('name','','trim');
        $reqData['have_menu']=$this->request->post('have_menu','','trim');
        $dispatch = $this->request->dispatch();
        if (!isset($dispatch['user_no'])){
            json_encode(array('status'=>-1,'msg'=>'error','data'=>[]));
        }

        /****判断数据合法性****/
        $user=db('user a')
            ->join('rolo_admin b','a.admin_id=b.id','LEFT')
            ->field('a.admin_id,b.have_menu')
            ->where(['a.status'=>1,'b.status'=>1,'a.user_no'=>$dispatch['user_no']])
            ->find();
        $reqData['have_menu']=implode(',',array_intersect(explode(',',$reqData['have_menu']),explode(',',$user['have_menu'])));
        /****整理数据****/
        $insert['pid']=$user['admin_id'];
        $insert['name']=$reqData['name'];
        $insert['have_menu']=$reqData['have_menu'];
        $upd['name']=$reqData['name'];
        $upd['have_menu']=$reqData['have_menu'];
        /*****修改数据库******/
        Db::startTrans();
        try {
            if (empty($reqData['id'])){
                db('rolo_admin')->insert($insert);
            }else{
                db('rolo_admin')->where('id',$reqData['id'])->update($upd);
            }
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            $msg  = "File：" . $e->getFile() . "\n";
            $msg .= "Line：" . $e->getLine() . "\n";
            $msg .= "Message：" . $e->getMessage() . "\n";
            var_dump($msg);
            // 回滚事务
            Db::rollback();
            $this->result['status'] = -1;
            $this->result['msg'] = '失败';
            return $this->result;
        }
        return json_encode(array('status'=>1,'msg'=>'success','data'=>[]));
    }

    /**
     * 角色详情
     */
    public function crmRoloDetail()
    {
        $reqData['id']=$this->request->get('id',null,'intval');
        $dispatch = $this->request->dispatch();
        if (!isset($dispatch['user_no'])){
            return json_encode(array('status'=>-1,'msg'=>'error','data'=>[]));
        }
        $result=db('rolo_admin')
            ->where(['status'=>1,'id'=>$reqData['id']])
            ->find();
        return json_encode(array('status'=>1,'msg'=>'success','data'=>$result));
    }

    /**
     * 删除用户
     */
    public function crmDelUser()
    {
        $reqData['user_no']=$this->request->get('user_no',0,'intval');
        $dispatch = $this->request->dispatch();
        if (!isset($dispatch['user_no'])){
            return json_encode(array('status'=>-1,'msg'=>'error','data'=>[]));
        }
        /*****修改数据库******/
        $upd=['status'=>-1,'updated_at'=>time()];
        Db::startTrans();
        try {
            db('user')->where('user_no', $reqData['user_no'])->update($upd);
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            $msg = "File：" . $e->getFile() . "\n";
            $msg .= "Line：" . $e->getLine() . "\n";
            $msg .= "Message：" . $e->getMessage() . "\n";
            var_dump($msg);
            // 回滚事务
            Db::rollback();
            $this->result['status'] = -1;
            $this->result['msg'] = '失败';
            return $this->result;
        }
        return json_encode(array('status' => 1, 'msg' => 'success', 'data' => []));
    }

    /**
     * 删除角色
     */
    public function crmDelRolo()
    {
        $reqData['id']=$this->request->get('id',0,'intval');
        $dispatch = $this->request->dispatch();
        if (!isset($dispatch['user_no'])){
            return json_encode(array('status'=>-1,'msg'=>'error','data'=>[]));
        }
        /*****修改数据库******/
        $upd=['status'=>-1];
        Db::startTrans();
        try {
            db('rolo_admin')->where('id', $reqData['id'])->update($upd);
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            $msg = "File：" . $e->getFile() . "\n";
            $msg .= "Line：" . $e->getLine() . "\n";
            $msg .= "Message：" . $e->getMessage() . "\n";
            var_dump($msg);
            // 回滚事务
            Db::rollback();
            $this->result['status'] = -1;
            $this->result['msg'] = '失败';
            return $this->result;
        }
        return json_encode(array('status' => 1, 'msg' => 'success', 'data' => []));
    }

    /**
     * 园区企业内页的监控
     */
    public function insideMonitor()
    {
        $reqData['type']=$this->request->param('type','','trim');
        $reqData['identification']=$this->request->param('identification','','trim');
        $reqData['phone']=$this->request->param('phone','','trim');
        $reqData['email']=$this->request->param('email','','trim');
        $reqData['project_name']=$this->request->param('project_name','','trim');
        $dispatch = $this->request->dispatch();
        if (!isset($dispatch['user_no'])){
            return json_encode(array('status'=>-1,'msg'=>'error','data'=>[]));
        }
        /**完善数据库字段**/
        if ($reqData['type']=='company'){
            $reqData['s_type']=2;
        }elseif ($reqData['type']=='park'){
            $reqData['s_type']=10;
        }else{
            return json_encode(array('status'=>-1,'msg'=>'error','data'=>[]));
        }
        $reqData['contact']=db('user')->where(['status'=>1,'user_no'=>$dispatch['user_no']])->value('username');
        $reqData['user_no'] = $dispatch['user_no'];
        $reqData['created_at'] = $reqData['updated_at'] = time();
        $reqData['status'] = 1;
        $reqData['is_monitor']=3;
        /***主表和log表和审核资料表**/
        Db::startTrans();
        try {
            //主表
            unset($reqData['type']);
            $id=Db::table('cq_project')->insertGetId($reqData);
            //log
            $log['project_id'] = $id;
            $log['user_no'] = $dispatch['user_no'];
            $log['project_name'] = $reqData['project_name'];
            $log['contact'] = $reqData['contact'];
            $log['phone'] = $reqData['phone'];
            $log['email'] = $reqData['email'];
            $log['follow_status'] = 1;
            $log['is_monitor'] =$reqData['is_monitor'];
            $log['monited_at'] = 0;
            $log['status'] = 1;
            $log['created_at'] = $log['updated_at'] = time();
            Db::table('cq_project_history')->insert($log);
            //审核资料表
            $material['project_id']=$id;
            $material['company_name']=$reqData['project_name'];
            $material['phone']=$reqData['phone'];
            $material['email']=$reqData['email'];
            $material['status']=3;
            $material['user_no']=$dispatch['user_no'];
            $material['created_at']=$material['updated_at']=date('Y-m-d H:i:s',time());
            Db::table('cq_project_monit')->insert($material);
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            $msg = "File：" . $e->getFile() . "\n";
            $msg .= "Line：" . $e->getLine() . "\n";
            $msg .= "Message：" . $e->getMessage() . "\n";
            var_dump($msg);
            // 回滚事务
            Db::rollback();
            $this->result['status'] = -1;
            $this->result['msg'] = '失败';
            return $this->result;
        }
        return json_encode(array('status' => 1, 'msg' => 'success', 'data' => []));
    }
}